﻿<%@ WebHandler Language="C#" Class="FileHandle" %>

using System;
using System.Web;

public class FileHandle : IHttpHandler
{
    DAO.Workspace DAOWorkspace;
    DAO.Database DAODatabase;
    DAO.DBEngine DAODBEngine = new DAO.DBEngine();
    const DAO.DataTypeEnum dbText = DAO.DataTypeEnum.dbText;
    DAO.TableDef DAOTable;
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.ServerVariables["HTTP_Referer"] == null || context.Request.ServerVariables["REMOTE_HOST"] == null || context.Request.ServerVariables["SERVER_NAME"] != context.Request.Url.Host)
        {
            context.Response.Write("非法访问!");
            context.Response.End();
            return;
        }
        string action = context.Request.QueryString["ac"];
        switch (action)
        {
            case "renametb": Renametb(context); break;//修改数据表名称.
            case "changedesc": ChangeDbDesc(context); break;//修改说明
        }
    }


    private void Renametb(HttpContext context)
    {
        string old = context.Request.Form["oldname"];
        string newname = context.Request.Form["newname"];
        try
        {          
            MdbWatch.DbHelper.RenameTableName(old, newname);
            context.Response.Write("ok");
        }
        catch
        {
            context.Response.Write("error");
        }
    }
    private void ChangeDbDesc(HttpContext context)
    {
        // 创建一个工作区 
        DAOWorkspace = DAODBEngine.CreateWorkspace("WorkSpace", "admin", "", 2);
        // 打开数据库 
        string dbpath = HttpContext.Current.Server.MapPath("~/app_data/db.mdb");
        DAODatabase = DAOWorkspace.OpenDatabase(dbpath,false, false, null);
        string tablename = context.Request.Form["tbname"];
        string description = context.Request.Form["description"];

        DAOTable = DAODatabase.TableDefs[tablename];
        try
        {
            DAOTable.Properties["Description"].Value = description;            
        }
        catch
        {
            DAO.Property dbProperty = DAOTable.CreateProperty("Description", DAO.DataTypeEnum.dbText, description, false);
            DAOTable.Properties.Append(dbProperty);
            DAOTable.Properties.Refresh();                   
        }
        finally
        {
            DAODatabase.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(DAOTable);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(DAOWorkspace);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(DAODBEngine);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(DAODatabase);
            DAOTable = null;
            DAOWorkspace = null;
            DAOWorkspace = null;
            DAODatabase = null;
        }
        GC.WaitForPendingFinalizers();
        GC.Collect();
        context.Response.Write("ok");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}